package com.pds.web.operationLog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pds.common.utils.DateUtils;
import com.pds.web.operationLog.entity.PdsOperationLog;
import com.pds.web.operationLog.mapper.PdsOperationLogMapper;
import com.pds.web.operationLog.service.PdsOperationLogService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 用户业务操作日志表
 *
 * @author srf sunlightcs@gmail.com
 * @since 1.0.0 2024-02-07
 */
@Service("pdsOperationLogService")
public class PdsOperationLogServiceImpl extends ServiceImpl<PdsOperationLogMapper, PdsOperationLog> implements PdsOperationLogService {
    @Resource
    private PdsOperationLogMapper pdsOperationLogMapper;

    @Override
    public List<PdsOperationLog> selectList(PdsOperationLog pdsOperationLog) {
        QueryWrapper<PdsOperationLog> wrapper = new QueryWrapper<>();
        String userName = pdsOperationLog.getUserName();
        String operationIp = pdsOperationLog.getOperationIp();
        if(!Objects.isNull(userName) && !userName.isEmpty()){
            wrapper.like("user_name",userName);
        }
        if(!Objects.isNull(operationIp) && !operationIp.isEmpty()){
            wrapper.like("operation_ip",operationIp);
        }
        return pdsOperationLogMapper.selectList(wrapper);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public PdsOperationLog insert(PdsOperationLog pdsOperationLog) {
        Date date = DateUtils.parseDate(DateUtils.getTime());
        pdsOperationLog.setCreateTime(date);
        pdsOperationLogMapper.insert(pdsOperationLog);
        return pdsOperationLog;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public Boolean deleteById(Long id) {
        return pdsOperationLogMapper.deleteById(id) > 0;
    }
}